// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   QuestionServiceImp.java

package com.iphaeton.lms.business.exam;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.certificate.Certificate;
import com.iphaeton.lms.pojo.exam.*;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.log4j.Logger;

// Referenced classes of package com.iphaeton.lms.business.exam:
//            IQuestionService

public class QuestionServiceImp extends PersistentServiceSupport
    implements IQuestionService
{

    public QuestionServiceImp()
    {
        cal = Calendar.getInstance();
        time = cal.getTime();
        df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        currentTime = df.format(time);
    }

    public Question persistent(Question q)
        throws BusinessException
    {
        try
        {
            q = (Question)hbpservice.store(q);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6301\u4E45\u5316\u8BD5\u9898\u5931\u8D25\uFF01");
        }
        return q;
    }

    public Question findQuestionById(String id)
        throws BusinessException
    {
        Question q = null;
        try
        {
            q = (Question)hbpservice.get(id, com.iphaeton.lms.pojo.exam.Question.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u901A\u8FC7ID\u67E5\u8BE2\u8BD5\u9898\u5931\u8D25!");
        }
        return q;
    }

    public Paper findPaperById(String id)
        throws BusinessException
    {
        Paper p = null;
        try
        {
            p = (Paper)hbpservice.get(id, com.iphaeton.lms.pojo.exam.Paper.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u901A\u8FC7ID\u67E5\u8BE2\u8BD5\u5377\u5931\u8D25!");
        }
        return p;
    }

    public QuestionSort findQuestionSortById(String id)
        throws BusinessException
    {
        QuestionSort qs = null;
        try
        {
            qs = (QuestionSort)hbpservice.get(id, com.iphaeton.lms.pojo.exam.QuestionSort.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u901A\u8FC7ID\u67E5\u8BE2\u8BD5\u9898\u7C7B\u578B\u5931\u8D25!");
        }
        return qs;
    }

    public void removeQuestion(String id)
        throws BusinessException
    {
        Question q = findQuestionById(id);
        q.setState(new Integer(2));
        persistent(q);
    }

    public List queryQuestion(String sort_id, String hard_id, String course_id, String user_id, String station_id, String duty_id)
        throws BusinessException
    {
        List list = null;
        String hql = "from Question q where q.state!=2 ";
        if(!"".equals(sort_id))
            hql = hql + " and q.sortId='" + sort_id + "'";
        if(!"".equals(hard_id))
            hql = hql + " and q.hardId='" + hard_id + "'";
        if(!"".equals(course_id))
            hql = hql + " and q.courseId='" + course_id + "'";
        if(!"".equals(user_id))
            hql = hql + " and q.userId='" + user_id + "'";
        if(!"".equals(station_id))
            hql = hql + " and q.stationId='" + station_id + "'";
        if(!"".equals(duty_id))
            hql = hql + " and q.dutyId='" + duty_id + "'";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8BD5\u9898\u5217\u8868\u5931\u8D25!");
        }
        return list;
    }

    public List queryQuestionHard()
        throws BusinessException
    {
        String hql = "from QuestionHard qh";
        List list = null;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8BD5\u9898\u96BE\u5EA6\u5217\u8868\u51FA\u9519!");
        }
        return list;
    }

    public List queryQuestionSort()
        throws BusinessException
    {
        String hql = "from QuestionSort qs";
        List list = null;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8BD5\u9898\u5206\u7C7B\u5217\u8868\u51FA\u9519!");
        }
        return list;
    }

    public Paper createPaper(Paper p)
        throws BusinessException
    {
        try
        {
            p = (Paper)hbpservice.store(p);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6301\u4E45\u5316\u8BD5\u5377\u57FA\u672C\u4FE1\u606F\u51FA\u9519!");
        }
        return p;
    }

    public void questionToPaperByManual(String question_id, String paper_id)
        throws BusinessException
    {
        String checkSql = "select * from t_paper_question where paper_id='" + paper_id + "' and question_id='" + question_id + "'";
        String sql = "insert into t_paper_question (question_id,paper_id,score) values ('" + question_id + "','" + paper_id + "',0)";
        List list = null;
        try
        {
            list = jdbcpservice.query(checkSql);
            if(list.size() == 0 || list == null)
                jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6DFB\u52A0\u8BD5\u9898\u5230\u8BD5\u5377\u65F6\u53D1\u751F\u9519\u8BEF!");
        }
    }

    public List queryPaperSort(String paper_id)
        throws BusinessException
    {
        List list = null;
        String sql = "select count(q.id) as count,qs.id as sid,qs.name as sname from t_paper_question pq inner join t_question q on q.id = pq.question_id and pq.paper_id = '" + paper_id + "'" + " inner join t_question_sort qs on q.sort_id = qs.id " + "group by qs.name,qs.id order by sid ,count";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8BD5\u5377\u8BD5\u9898\u7C7B\u578B\u65F6\u51FA\u9519!");
        }
        return list;
    }

    public int sortTotal(String p_id, String s_id)
        throws BusinessException
    {
        int count = 0;
        String sql = "select count(q.id) as count  from t_paper_question pq  inner join t_question q on q.id = pq.question_id and pq.paper_id = '" + p_id + "'" + " inner join t_question_sort qs on q.sort_id = qs.id and qs.id='" + s_id + "'";
        try
        {
            count = ((Integer)((Map)jdbcpservice.query(sql).get(0)).get("count")).intValue();
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7ED9\u5B9A\u7ED9\u7C7B\u7ED9\u5B9A\u7C7B\u578B\u8BD5\u9898\u6570\u91CF\u662F\u51FA\u9519!");
        }
        return count;
    }

    public void assignScore(String p_id, String s_id, int total_score, float score)
        throws BusinessException
    {
        String sql1 = "update t_paper set score='" + total_score + "' where id='" + p_id + "'";
        String sql = "update t_paper_question set score='" + score + "' where paper_id='" + p_id + "' and question_id in " + "(select q.id from t_paper_question pq " + " inner join t_question q on q.id=pq.question_id and pq.paper_id='" + p_id + "' " + " inner join t_question_sort qs on qs.id='" + s_id + "' and q.sort_id=qs.id)";
        try
        {
            jdbcpservice.execute(sql1);
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u7ED9\u8BD5\u5377\u7ED9\u5B9A\u7C7B\u578B\u8BD5\u9898\u5206\u914D\u5206\u6570\u65F6\u51FA\u9519!");
        }
    }

    public void assignCourse(String p_id, String c_id)
        throws BusinessException
    {
        String sql = "insert into t_exam_course (paper_id,course_id) values ('" + p_id + "','" + c_id + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8003\u8BD5\u76F8\u5173\u8BFE\u7A0B\u65F6\u51FA\u9519!");
        }
    }

    public void assignTrain(String p_id, String t_id)
        throws BusinessException
    {
        String sql = "insert into t_exam_train (paper_id,train_id) values ('" + p_id + "','" + t_id + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8003\u8BD5\u76F8\u5173\u57F9\u8BAD\u73ED\u65F6\u51FA\u9519!");
        }
    }

    public void assignCertificate(String p_id, String c_id)
        throws BusinessException
    {
        String sql = "update t_certificate set paper_id='" + p_id + "' , state=1 where id='" + c_id + "'";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8003\u8BD5\u76F8\u5173\u8BA4\u8BC1\u65F6\u51FA\u9519!");
        }
    }

    public void assignGroup(String p_id, String g_id)
        throws BusinessException
    {
        String sql = "insert into t_exam_group (paper_id,group_id) values ('" + p_id + "','" + g_id + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8003\u8BD5\u76F8\u5173\u7528\u6237\u7EC4\u65F6\u51FA\u9519!");
        }
    }

    public void assignStudent(String p_id, String u_id)
        throws BusinessException
    {
        String sql = "insert into t_exam_user (paper_id,user_id) values ('" + p_id + "','" + u_id + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8003\u8BD5\u76F8\u5173\u7528\u6237\u65F6\u51FA\u9519!");
        }
    }

    public void assignOrganize(String p_id, String o_id)
        throws BusinessException
    {
        String sql = "insert into t_exam_organize(paper_id,organize_id) values ('" + p_id + "','" + o_id + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8003\u8BD5\u76F8\u5173\u7EC4\u7EC7\u65F6\u51FA\u9519!");
        }
    }

    public List queryPaperQuestion(String p_id)
        throws BusinessException
    {
        List list = null;
        String sql = "select qs.name as sname, q.name as question, q.prepare_answer as prepare_answer from t_question q inner join t_paper_question pq on pq.paper_id = '" + p_id + "' and pq.question_id = q.id " + "inner join t_question_sort qs on q.sort_id = qs.id " + "group by qs.id,qs.name,q.name,q.prepare_answer order by qs.id";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8BD5\u5377\u8BD5\u9898\u65F6\u53D1\u751F\u9519\u8BEF!");
        }
        return list;
    }

    public Pagination selectPaper(String userId, String row, int state, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(p.id) as count from Paper p where p.creater='" + userId + "' and p.deleted!=2 ";
        String hqlQueryStr = "from Paper p where p.creater='" + userId + "' and p.deleted!=2 ";
        if(state != 0)
        {
            selectHql = selectHql + " and p.sort='" + state + "'";
            hqlQueryStr = hqlQueryStr + " and p.sort='" + state + "'";
        }
        if(row != null && !"".equals(row))
            hqlQueryStr = hqlQueryStr + " order by p." + row;
        else
            hqlQueryStr = hqlQueryStr + " order by p.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find paper!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public Pagination examCompleted(String userId, String orgId, String row, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(p.id) as count from Paper p where state=1 and p.deleted!=2 and  (p.creater='" + userId + "' or p.organizeId='" + orgId + "') ";
        String hqlQueryStr = "from Paper p where state=1 and p.deleted!=2 and (p.creater='" + userId + "' or p.organizeId ='" + orgId + "') ";
        if(row != null && !"".equals(row))
            hqlQueryStr = hqlQueryStr + " order by p." + row;
        else
            hqlQueryStr = hqlQueryStr + " order by p.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find paper!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public void removePaper(String paper_id)
        throws BusinessException
    {
        Paper p = null;
        try
        {
            p = findPaperById(paper_id);
            p.setDeleted(new Integer(2));
            hbpservice.store(p);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u8003\u8BD5\u8BD5\u5377\u65F6\u51FA\u9519!");
        }
    }

    public Pagination examScore(String userId, String row, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(p.id) as count from Paper p where p.creater='" + userId + "' and p.startTime<='" + currentTime + "' and p.type=1  and p.deleted!=2 ";
        String hqlQueryStr = "from Paper p where p.creater='" + userId + "'  and p.startTime<='" + currentTime + "' and p.type=1  and p.deleted!=2 ";
        if(row != null && !"".equals(row))
            hqlQueryStr = hqlQueryStr + " order by p." + row;
        else
            hqlQueryStr = hqlQueryStr + " order by p.startTime";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find paper!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public List queryExamStudent(String id)
        throws BusinessException
    {
        String sql = "select distinct (u.id) as id, u.user_id as user_id, u.name as uname ,o.url as oname,us.exam_date as date from t_user u inner join t_organize o on u.organize_id=o.id inner join t_user_score us on u.id = us.user_id and us.paper_id ='" + id + "' and us.state=2";
        List list = null;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8003\u8BD5\u5B66\u5458\u5217\u8868\u65F6\u51FA\u9519: " + e.getMessage() + "!");
        }
        return list;
    }

    public List queryExamUserScore(String id)
        throws BusinessException
    {
        List list = null;
        String sql = "select u.user_id as user_id,u.name as user_name,us.score as score,o.url as url,us.exam_date as date  from t_user u inner join t_organize o on u.organize_id=o.id inner join t_user_score us on u.id=us.user_id and us.paper_id='" + id + "' and us.state=1";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5B66\u5458\u6210\u7EE9\u65F6\u51FA\u9519: " + e.getMessage() + "!");
        }
        return list;
    }

    public List queryMyScore(String id)
        throws BusinessException
    {
        List list = null;
        String sql = "select p.id as id,p.name as name,p.type as type,p.sort as sort,o.url as url,us.score as score from t_paper p inner join t_organize o on p.organize_id=o.id inner join t_user_score us on us.user_id='" + id + "' and us.paper_id=p.id and us.state=1 order by us.exam_date desc";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7528\u6237\u8003\u8BD5\u6210\u7EE9\u65F6\u51FA\u9519: " + e.getMessage() + "!");
        }
        return list;
    }

    public Certificate findPaperCertificate(String id)
        throws BusinessException
    {
        Certificate cert = null;
        String hql = "from Certificate c where c.paperId='" + id + "'";
        List list;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8003\u8BD5\u76F8\u5173\u8BA4\u8BC1\u65F6\u51FA\u9519!");
        }
        if(list != null && list.size() == 1)
            cert = (Certificate)list.get(0);
        return cert;
    }

    public List findPageList(Pagination pageInfo)
    {
        List list = null;
        try
        {
            list = getSpecifiedItems(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    private static Logger logger;
    Calendar cal;
    Date time;
    DateFormat df;
    String currentTime;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */
    static Class class$2; /* synthetic field */
    static Class class$3; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.exam.QuestionServiceImp.class.getName());
    }
}
